const Koa = require("koa");
const app = new Koa();
const router = require("koa-router")();
const cors = require("koa-cors");
app.use(cors());
const parser = require("koa-parser");
app.use(parser());

let studentList = [{
    name: '赵子琴',
    class: '高职计算机1班',
    mat: 96,
    chn: 93,
    eng: 87
}, {
    name: '钱丑棋',
    class: '高职计算机1班',
    mat: 54,
    chn: 62,
    eng: 38
}, {
    name: '孙寅书',
    class: '高职计算机1班',
    mat: 93,
    chn: 95,
    eng: 98
}, {
    name: '李卯画',
    class: '高职计算机1班',
    mat: 68,
    chn: 98,
    eng: 76
}, {
    name: '周辰笔',
    class: '高职计算机1班',
    mat: 88,
    chn: 75,
    eng: 62
}, {
    name: '吴巳墨',
    class: '高职计算机1班',
    mat: 79,
    chn: 89,
    eng: 99
}, {
    name: '郑午纸',
    class: '高职计算机1班',
    mat: 98,
    chn: 88,
    eng: 78
}, {
    name: '王未砚',
    class: '高职计算机1班',
    mat: 94,
    chn: 85,
    eng: 76
}, {
    name: '冯申梅',
    class: '高职计算机1班',
    mat: 100,
    chn: 94,
    eng: 98
}, {
    name: '陈酉兰',
    class: '高职计算机1班',
    mat: 83,
    chn: 84,
    eng: 85
}, {
    name: '褚戌竹',
    class: '高职计算机1班',
    mat: 97,
    chn: 97,
    eng: 68
}, {
    name: '卫亥菊',
    class: '高职计算机1班',
    mat: 60,
    chn: 77,
    eng: 90
}]

router.get("/student", async ctx => {
    ctx.body = studentList
})
router.post("/student", async ctx => {
    let student = ctx.request.body.student;
    studentList.push(student)
    ctx.body = studentList
})
router.delete("/student/:id", async ctx => {
    let id = ctx.params.id;
    studentList.splice(id,1)
    ctx.body = studentList
})
router.put("/student/:id", async ctx => {
    let student = ctx.request.body.student;
    let id = ctx.params.id;
    studentList.splice(id,1,student)
    ctx.body = studentList
})

app.use(router.routes());
app.listen(3000, () => {
    console.log("server is running 3000")
})